package com.example.basicdemo.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.basicdemo.model.domain.AclRsp;
import com.example.basicdemo.model.domain.InnerData;
import com.isyscore.os.dev.config.IsyscoreConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该控制器提供了功能菜单获取接口 PS.功能权限接入看这里
 * 该案例为 <系统管理>应用 接入功能权限的实际案例
 */

@RestController
@RequestMapping("/api/${api-module}/demo/acl")
public class AclController {

    private static Map<String, String> exField = new HashMap<>();
    private static String exFieldStr;

    static {
        exField.put("belong", "operation-center");
        exFieldStr = JSONObject.toJSONString(exField);
    }

    @Autowired
    private IsyscoreConfigProperties isyscoreConfigProperties;

    /**
     * 启动工作流
     */
    @PostMapping()
    public List<AclRsp> getAcl() {
        List<AclRsp> aclRspList = new ArrayList<>();
        AclRsp aclRsp = new AclRsp();
        aclRsp.setAppCode(isyscoreConfigProperties.getAppCode());
        aclRsp.setAppName(isyscoreConfigProperties.getAppName());
        aclRsp.setType(0);
        aclRsp.setRootSelect(true);

        List<InnerData> acls = new ArrayList<>();
        // 监控
        acls.add(getMonitorAcl());
        // 日志
        acls.add(getLogAcl());
        // 日志
        acls.add(getAlertAcl());
        // 应用
        acls.add(getAppAcl());
        // 系统
        acls.add(getSystemAcl());

        aclRsp.setAcls(acls);
        aclRspList.add(aclRsp);
        return aclRspList;
    }


    private InnerData getMonitorAcl() {
        InnerData aclRsp = new InnerData();
        aclRsp.setCode("monitor");
        aclRsp.setName("监控");
        aclRsp.setSeq(1);
        aclRsp.setType(1);

        List<InnerData> acls = new ArrayList<>();
        InnerData item = new InnerData();
        item.setCode("monitor-system-overview");
        item.setName("系统概览");
        item.setSeq(0);
        item.setStatus(1);
        item.setType(1);
        acls.add(item);

        InnerData item2 = new InnerData();
        item2.setCode("monitor-host");
        item2.setName("主机");
        item2.setSeq(1);
        item2.setStatus(1);
        item2.setType(1);
        acls.add(item2);

        InnerData item3 = new InnerData();
        item3.setCode("monitor-database");
        item3.setName("数据库");
        item3.setSeq(2);
        item3.setStatus(1);
        item3.setType(1);
        item3.setExField(exFieldStr);
        acls.add(item3);


        InnerData item6 = new InnerData();
        item6.setCode("monitor-link");
        item6.setName("调用链路");
        item6.setSeq(5);
        item6.setStatus(1);
        item6.setType(1);
        item6.setExField(exFieldStr);
        acls.add(item6);


        List<InnerData> serviceAcls = new ArrayList<>();
        InnerData item5 = new InnerData();
        item5.setCode("monitor-service");
        item5.setName("服务监控");
        item5.setSeq(4);
        item5.setStatus(1);
        item5.setType(1);
        item5.setExField(exFieldStr);
        acls.add(item5);

        InnerData item7 = new InnerData();
        item7.setCode("monitor-service-total");
        item7.setName("服务总况");
        item7.setSeq(0);
        item7.setStatus(1);
        item7.setType(1);
        item5.setAclList(serviceAcls);
        serviceAcls.add(item7);

        InnerData item8 = new InnerData();
        item8.setCode("monitor-service-api");
        item8.setName("接口查询");
        item8.setSeq(1);
        item8.setStatus(1);
        item8.setType(1);
        serviceAcls.add(item8);

        InnerData item9 = new InnerData();
        item9.setCode("monitor-service-event");
        item9.setName("事件查询");
        item9.setSeq(2);
        item9.setStatus(1);
        item9.setType(1);
        serviceAcls.add(item9);


        aclRsp.setAclList(acls);
        return aclRsp;
    }

    private InnerData getLogAcl() {
        InnerData aclRsp = new InnerData();
        aclRsp.setCode("log");
        aclRsp.setName("日志");
        aclRsp.setSeq(1);
        aclRsp.setType(1);

        List<InnerData> acls = new ArrayList<>();
        InnerData item = new InnerData();
        item.setCode("log-download");
        item.setName("日志中心");
        item.setSeq(0);
        item.setStatus(1);
        item.setType(1);
        acls.add(item);

        InnerData item2 = new InnerData();
        item2.setCode("log-visualization");
        item2.setName("可视化表格");
        item2.setSeq(1);
        item2.setStatus(1);
        item2.setType(1);
        item2.setExField(exFieldStr);
        acls.add(item2);

        aclRsp.setAclList(acls);
        return aclRsp;
    }

    private InnerData getAlertAcl() {
        InnerData aclRsp = new InnerData();
        aclRsp.setCode("alert");
        aclRsp.setName("告警");
        aclRsp.setSeq(2);
        aclRsp.setType(1);
        aclRsp.setExField(exFieldStr);

        List<InnerData> acls = new ArrayList<>();
        InnerData item = new InnerData();
        item.setCode("alert-contact");
        item.setName("联系人");
        item.setSeq(0);
        item.setStatus(1);
        item.setType(1);
        item.setExField(exFieldStr);
        acls.add(item);

        InnerData item2 = new InnerData();
        item2.setCode("alert-rule");
        item2.setName("告警规则");
        item2.setSeq(1);
        item2.setStatus(1);
        item2.setType(1);
        item2.setExField(exFieldStr);
        acls.add(item2);

        InnerData item3 = new InnerData();
        item3.setCode("alert-strategy");
        item3.setName("通知策略");
        item3.setSeq(2);
        item3.setStatus(1);
        item3.setType(1);
        item3.setExField(exFieldStr);
        acls.add(item3);

        InnerData item4 = new InnerData();
        item4.setCode("alert-history");
        item4.setName("告警历史");
        item4.setSeq(3);
        item4.setStatus(1);
        item4.setType(1);
        item4.setExField(exFieldStr);
        acls.add(item4);

        aclRsp.setAclList(acls);
        return aclRsp;
    }

    private InnerData getAppAcl() {
        InnerData aclRsp = new InnerData();
        aclRsp.setCode("app");
        aclRsp.setName("应用");
        aclRsp.setSeq(3);
        aclRsp.setType(1);

        List<InnerData> acls = new ArrayList<>();
        InnerData item = new InnerData();
        item.setCode("app-manager");
        item.setName("应用管理");
        item.setSeq(0);
        item.setStatus(1);
        item.setType(1);
        acls.add(item);


        InnerData item2 = new InnerData();
        item2.setCode("app-cloning");
        item2.setName("应用克隆");
        item2.setSeq(1);
        item2.setStatus(1);
        item2.setType(1);
        acls.add(item2);


        aclRsp.setAclList(acls);
        return aclRsp;
    }

    private InnerData getSystemAcl() {
        InnerData aclRsp = new InnerData();
        aclRsp.setCode("system");
        aclRsp.setName("系统");
        aclRsp.setSeq(4);
        aclRsp.setType(1);

        List<InnerData> acls = new ArrayList<>();
        InnerData item = new InnerData();
        item.setCode("system-remote-ops");
        item.setName("远程协助");
        item.setSeq(0);
        item.setStatus(1);
        item.setType(1);
        acls.add(item);

        InnerData item2 = InnerData.builder()
                .code("system-checker")
                .name("OS巡检")
                .seq(1)
                .status(1)
                .type(1)
                .exField(exFieldStr)
                .build();
        acls.add(item2);

        InnerData item3 = new InnerData();
        item3.setCode("system-dev-param");
        item3.setName("开发参数");
        item3.setSeq(2);
        item3.setStatus(1);
        item3.setType(1);
        item3.setExField(exFieldStr);
        acls.add(item3);

        InnerData item4 = new InnerData();
        item4.setCode("system-cert-center");
        item4.setName("证书中心");
        item4.setSeq(3);
        item4.setStatus(1);
        item4.setType(1);
        item4.setExField(exFieldStr);
        acls.add(item4);

        aclRsp.setAclList(acls);
        return aclRsp;
    }


}
